package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.BusinessCooperation;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.BusinessCooperationService;
import co.yixiang.modules.system.dto.BusinessCooperationDto;
import co.yixiang.modules.system.dto.queryCriteria.BusinessCooperationQueryCriteria;
import co.yixiang.modules.system.mapper.BusinessCooperationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author db
* @date 2020-11-19
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "businessCooperation")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BusinessCooperationServiceImpl extends BaseServiceImpl<BusinessCooperationMapper, BusinessCooperation> implements BusinessCooperationService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(BusinessCooperationQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<BusinessCooperation> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(BusinessCooperationDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<BusinessCooperation> queryAll(BusinessCooperationQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(BusinessCooperation.class, criteria));
    }


    @Override
    public void download(List<BusinessCooperationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BusinessCooperationDto businessCooperation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", businessCooperation.getName());
            map.put("手机", businessCooperation.getPhone());
            map.put("邮箱", businessCooperation.getEmail());
            map.put("行业", businessCooperation.getBusiness());
            map.put("意见", businessCooperation.getAdvise());
            map.put("创建时间", businessCooperation.getCreateTimes());
            map.put("修改时间", businessCooperation.getUpdateTimes());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Object join(BusinessCooperationDto resources) {
        BusinessCooperation businessCooperation = BusinessCooperation.builder()
                .name(resources.getName())
                .phone(resources.getPhone())
                .email(resources.getEmail())
                .business(resources.getBusiness())
                .advise(resources.getAdvise())
                .build();

        save(businessCooperation);

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 200);
        resMap.put("message", "提交成功");
        resMap.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));

        return resMap;
    }
}
